//@Lim Wei Ke A0083402R
package logic;

import java.util.ArrayList;

import storage.Storage;
import global.CommandType.Type;

public class RedoCommand extends Command {

	private boolean isExecutable;
	private String feedback;

	public RedoCommand(boolean isExecutable, String feedback) {
		this.isExecutable = isExecutable;
		this.feedback = feedback;
	}

	public ArrayList<String> execute(){
		ArrayList<String> newFeedback = new ArrayList<String>();
		
		if (isExecutable){
			try {
				DisplayCommand lastDisplayed;
				if (ExecutionHandler.getIsUndo()) {
					switch (ExecutionHandler.getMostRecentCommandType()) {
					case ADD:
						for (int i = 0; i < ExecutionHandler.getOldTasks().size(); i++)
							Storage.addFile(ExecutionHandler.getOldTasks().get(i));
						feedback = "\"Add\" command redo.";
						lastDisplayed = ExecutionHandler.getSecondRecentDisplay();
						newFeedback = lastDisplayed.display();
						break;
						
					case DELETE:
						for (int i = 0; i < ExecutionHandler.getOldTasks().size(); i++) 
							Storage.deleteFile(ExecutionHandler.getOldTasks().get(i).getTaskFileName());
						feedback = "\"Delete\" command redo.";
						lastDisplayed = ExecutionHandler.getSecondRecentDisplay();
						newFeedback = lastDisplayed.display();
						break;
						
					case UPDATE: 
						Storage.deleteFile(ExecutionHandler.getUpdatedTasks().get(0).getTaskFileName());
						Storage.addFile(ExecutionHandler.getOldTasks().get(0));					
						feedback = "\"Update\" command redo.";
						lastDisplayed = ExecutionHandler.getSecondRecentDisplay();
						newFeedback = lastDisplayed.display();
						break;
						
					case MARKDONE:
						for (int i = 0; i < ExecutionHandler.getUpdatedTasks().size(); i++)
							Storage.deleteFile(ExecutionHandler.getUpdatedTasks().get(i).getTaskFileName());
						for (int i = 0; i < ExecutionHandler.getOldTasks().size(); i++)
							Storage.addFile(ExecutionHandler.getOldTasks().get(i));
						feedback = "\"Markdone\" command redo.";
						lastDisplayed = ExecutionHandler.getSecondRecentDisplay();
						newFeedback = lastDisplayed.display();
						break;
						
					case MARKUNDONE:
						for (int i = 0; i < ExecutionHandler.getUpdatedTasks().size(); i++)
							Storage.deleteFile(ExecutionHandler.getUpdatedTasks().get(i).getTaskFileName());
						for (int i = 0; i < ExecutionHandler.getOldTasks().size(); i++)
							Storage.addFile(ExecutionHandler.getOldTasks().get(i));
						feedback = "\"MarkUndone\" command redo.";
						lastDisplayed = ExecutionHandler.getSecondRecentDisplay();
						newFeedback = lastDisplayed.display();
						break;
					default: 
						feedback = "Unknown error.";
					}

				} else {
					feedback = "Unable to redo as there is no undo action done.";
					lastDisplayed = ExecutionHandler.getRecentDisplay();
					newFeedback = lastDisplayed.display();

				}
				ExecutionHandler.setMostRecentCommandType(Type.REDO);
				ExecutionHandler.setIsUndo(false);
			}catch(Exception e){
				newFeedback.add("Redo unsuccessful");
			}
		}
		newFeedback.add(feedback);
		return newFeedback;
	}
	
	public String getTestFeedback() {
		return "redo";
	}
}
//@Lim Wei Ke A0083402R